﻿var maid = "";
$(function () {
    $("#ddlKhoa").on("change", function () {
        dropdownlist_Phong("#ddlPhong", $(this).val(), "", false);
        $("#ddlPhong").parent().find(".dropdown-toggle").focus();
    });
    $("#ddlKhoa_Moi").on("change", function () {
        dropdownlist_Phong("#ddlPhong_Moi", $(this).val(), "", false);
        $("#ddlPhong_Moi").parent().find(".dropdown-toggle").focus();
    });
    $("#div_phanquyen, #div_chuyencongtac").on("hide", function () {
        window.__doPostBack("LoadPage", "");
    });
});

function phanquyenkhoaphong(tag_a) {
    $("#txtUserName").text($(tag_a).attr("user"));
    dropdownlist_Khoa("#ddlKhoa", "", false);
    dropdownlist_Phong("#ddlPhong", $("#ddlKhoa option:selected").val(), "", false);
    tablechucvu("#tb_chucvu");
    $("#div_phanquyen").modal({
        show: true,
        focusOn: $("#ddlKhoa").parent().find(".dropdown-toggle")
    });
}

function chuyencongtac(tag_a) {
    $("#txtUserName_ChuyenCongTac").text($(tag_a).attr("user"));
    maid = $(tag_a).attr("maid");
    var dataChucVuCu = TaiKhoan_ChucVu_PhongBan.SelectById(maid);
    var makhoacu = "", maphongcu = "", machucvucu = "";
    if (dataChucVuCu.length > 0) {
        makhoacu = dataChucVuCu[0].MaKhoa;
        maphongcu = dataChucVuCu[0].MaPhongBan;
        machucvucu = dataChucVuCu[0].MaChucVu;
    }
    dropdownlist_Khoa("#ddlKhoa_Cu", makhoacu, true);
    dropdownlist_Phong("#ddlPhong_Cu", $("#ddlKhoa_Cu option:selected").val(), maphongcu, true);
    tablechucvu_cu("#tb_chucvu_cu", machucvucu);
    // Chức vụ mới
    dropdownlist_Khoa("#ddlKhoa_Moi", "", false);
    dropdownlist_Phong("#ddlPhong_Moi", $("#ddlKhoa_Moi option:selected").val(), "", false);
    tablechucvu("#tb_chucvu_moi");
    $("#div_chuyencongtac").modal({
        show: true
    });
}

function luuphanquyen() {
    if (CheckSession() === "") {
        window.location = "/";
        return;
    }
    $("#ddlKhoa").each(function () {
        $(this).rules("add", {
            required: true,
            messages: {
                required: MsErrorEmpty
            }
        });
    });
    if (!$("#form_phanquyen").valid()) return;
    var statusTaiKhoan = TrangThaiTaiKhoan($("#txtUserName").text());
    if (statusTaiKhoan === "") {
        var listchucvu = $("#tb_chucvu").bootstrapTable("getSelections");
        if (listchucvu.length > 0) {
            $.each(listchucvu, function (key, value) {
                TaiKhoan_ChucVu_PhongBan.PhanQuyenTaiKhoan($("#txtUserName").text(), $("#ddlKhoa option:selected").val(), $("#ddlPhong option:selected").val(), value.MaChucVu);
            });
        } else {
            TaiKhoan_ChucVu_PhongBan.PhanQuyenTaiKhoan($("#txtUserName").text(), $("#ddlKhoa option:selected").val(), $("#ddlPhong option:selected").val(), "");
        }
        $("#div_phanquyen").modal("hide");
    } else {
        bootbox.alert(statusTaiKhoan);
    }
}

function luuchuyencongtac() {
    if (CheckSession() === "") {
        window.location = "/";
        return;
    }
    $("#ddlKhoa_Moi").each(function () {
        $(this).rules("add", {
            required: true,
            messages: {
                required: MsErrorEmpty
            }
        });
    });
    if (!$("#form_chuyencongtac").valid()) return;
    var statusTaiKhoan = TrangThaiTaiKhoan($("#txtUserName_ChuyenCongTac").text());
    if (statusTaiKhoan === "") {
        TaiKhoan_ChucVu_PhongBan.UpdateIsActive(maid);
        var listchucvu = $("#tb_chucvu_moi").bootstrapTable("getSelections");
        if (listchucvu.length > 0) {
            $.each(listchucvu, function (key, value) {
                TaiKhoan_ChucVu_PhongBan.PhanQuyenTaiKhoan($("#txtUserName_ChuyenCongTac").text(), $("#ddlKhoa_Moi option:selected").val(), $("#ddlPhong_Moi option:selected").val(), value.MaChucVu);
            });
        } else {
            TaiKhoan_ChucVu_PhongBan.PhanQuyenTaiKhoan($("#txtUserName_ChuyenCongTac").text(), $("#ddlKhoa_Moi option:selected").val(), $("#ddlPhong_Moi option:selected").val(), "");
        }
        maid = "";
        $("#div_chuyencongtac").modal("hide");
    } else {
        bootbox.alert(statusTaiKhoan);
    }
}

function dropdownlist_Khoa(id_dropdown, makhoa, isdisabled) {
    $(id_dropdown).empty();
    $(id_dropdown).append("<option value=''>Chọn</option>");
    var data = SelectAllKhoa();
    if (data.length > 0) {
        $.each(data, function (key, value) {
            $(id_dropdown).append("<option value='" + value.MaKhoa + "'>" + value.TenKhoa + "</option>");
        });
    }
    $(id_dropdown).find("option[value='" + makhoa + "']").prop("selected", "true");
    if (isdisabled) $(id_dropdown).prop("disabled", "true");
    $(id_dropdown).selectpicker("refresh");
}

function dropdownlist_Phong(id_dropdown, makhoa, maphong, isdisabled) {
    $(id_dropdown).empty();
    $(id_dropdown).append("<option value=''>Chọn</option>");
    var data = SelectPhongBanByKhoa(makhoa);
    if (data.length > 0) {
        $.each(data, function (key, value) {
            $(id_dropdown).append("<option value='" + value.MaPhongBan + "'>" + value.TenPhongBan + "</option>");
        });
    }
    $(id_dropdown).find("option[value='" + maphong + "']").prop("selected", "true");
    if (isdisabled) $(id_dropdown).prop("disabled", "true");
    $(id_dropdown).selectpicker("refresh");
}

function tablechucvu(id_table) {
    $(id_table).bootstrapTable("destroy");
    $(id_table).bootstrapTable({
        classes: "table table-condensed",
        height: 300,
        data: SelectAllChucVu(),
        clickToSelect: true,
        cache: false,
        search: true,
        searchAlign: "right",
        formatShowingRows: function () {
            return "";
        },
        columns: [
            {
                checkbox: true
            },
            {
                field: "MaChucVu",
                visible: false
            },
            {
                field: "TenChucVu",
                title: "Chức vụ"
            }
        ]
    });
}

function tablechucvu_cu(id_table, chucvucu) {
    $(id_table).bootstrapTable("destroy");
    $(id_table).bootstrapTable({
        classes: "table table-condensed",
        height: 300,
        data: SelectAllChucVu(),
        singleSelect: true,
        cache: false,
        search: true,
        searchAlign: "right",
        formatShowingRows: function () {
            return "";
        },
        columns: [
            {
                field: "MaChucVu",
                checkbox: true,
                formatter: function (value, row, index) {
                    return {
                        disabled: true,
                        checked: value === chucvucu ? true : false
                    }
                }
            },
            {
                field: "TenChucVu",
                title: "Chức vụ"
            }
        ]
    });
}

var TaiKhoan_ChucVu_PhongBan = {
    ArrJson: [],
    PhanQuyenTaiKhoan: function (taikhoan, makhoa, maphongban, machucvu) {
        $.ajax({
            type: "POST",
            url: "/Webservice/QuanTriHeThong/ThietLapHeThong/wsTaiKhoan_ChucVu_PhongBan.asmx/PhanQuyenTaiKhoan",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{'taikhoan': '" + taikhoan + "', 'makhoa': '" + makhoa + "'" +
                ", 'maphongban': '" + maphongban + "', 'machucvu': '" + machucvu + "'}",
            async: false,
            success: function () {
            },
            error: function () {
                bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
            }
        });
    },
    SelectById: function (id) {
        $.ajax({
            type: "POST",
            url: "/Webservice/QuanTriHeThong/ThietLapHeThong/wsTaiKhoan_ChucVu_PhongBan.asmx/SelectById",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{'id': '" + id + "'}",
            async: false,
            success: function (n) {
                if (n.d !== "") {
                    if (n.d != null || n.d != undefined) {
                        TaiKhoan_ChucVu_PhongBan.ArrJson = $.parseJSON(n.d);
                    }
                }
            },
            error: function () {
                bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
            }
        });
        return TaiKhoan_ChucVu_PhongBan.ArrJson;
    },
    UpdateIsActive: function (id) {
        $.ajax({
            type: "POST",
            url: "/Webservice/QuanTriHeThong/ThietLapHeThong/wsTaiKhoan_ChucVu_PhongBan.asmx/UpdateIsActive",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{'id': '" + id + "'}",
            async: false,
            success: function () {
            },
            error: function () {
                bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
            }
        });
    }
};